import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  }
  // {
  //   path: "/askForLeaveManage",
  //   component: Layouts,
  //   redirect: "/askForLeaveManage/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/askForLeaveManage/index.vue"),
  //       name: "AskForLeaveManage",
  //       meta: {
  //         title: "请假管理",
  //         svgIcon: "link",
  //         alwaysShow: true
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/systemSettings",
  //   component: Layouts,
  //   redirect: "/systemSettings/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/systemSettings/index.vue"),
  //       name: "SystemSettings",
  //       meta: {
  //         title: "系统设置",
  //         svgIcon: "link",
  //         alwaysShow: true
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/notificationInfo",
  //   component: Layouts,
  //   redirect: "/notificationInfo/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/notificationInfo/index.vue"),
  //       name: "NotificationInfo",
  //       meta: {
  //         title: "通知公告",
  //         svgIcon: "unocss",
  //         alwaysShow: true
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/statisticalReport",
  //   component: Layouts,
  //   redirect: "/statisticalReport/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/statisticalReport/index.vue"),
  //       name: "StatisticalReport",
  //       meta: {
  //         title: "统计报表",
  //         svgIcon: "unocss",
  //         roles: ["admin"],
  //         alwaysShow: true
  //       }
  //     }
  //   ]
  // }
  // {
  //   path: "/askForLeaveSelect",
  //   component: Layouts,
  //   redirect: "/askForLeaveSelect/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/askForLeaveSelect/index.vue"),
  //       name: "AskForLeaveSelect",
  //       meta: {
  //         title: "请假记录查询",
  //         svgIcon: "unocss",
  //         roles: ["admin"],
  //         alwaysShow: true
  //       }
  //     }
  //   ]
  // }
  // {
  //   path: "/leaveApproval",
  //   component: Layouts,
  //   redirect: "/leaveApproval/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/leaveApproval/index.vue"),
  //       name: "LeaveApproval",
  //       meta: {
  //         title: "请假审批",
  //         svgIcon: "unocss",
  //         roles: ["admin"],
  //         alwaysShow: true
  //       }
  //     }
  //   ]
  // }
  // {
  //   path: "/table",
  //   component: Layouts,
  //   redirect: "/table/element-plus",
  //   name: "Table",
  //   meta: {
  //     title: "表格",
  //     elIcon: "Grid"
  //   },
  //   children: [
  //     {
  //       path: "element-plus",
  //       component: () => import("@/views/table/element-plus/index.vue"),
  //       name: "ElementPlus",
  //       meta: {
  //         title: "Element Plus",
  //         keepAlive: true
  //       }
  //     },
  //     {
  //       path: "vxe-table",
  //       component: () => import("@/views/table/vxe-table/index.vue"),
  //       name: "VxeTable",
  //       meta: {
  //         title: "Vxe Table",
  //         keepAlive: true
  //       }
  //     }
  //   ]
  // }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/manageAdmin",
    component: Layouts,
    redirect: "/manageAdmin/userManage",
    name: "ManageAdmin",
    meta: {
      title: "管理员管理",
      svgIcon: "lock",
      roles: ["admin"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "userManage",
        component: () => import("@/views/manageAdmin/userManage.vue"),
        name: "UserManage",
        meta: {
          title: "用户管理" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      },
      {
        path: "roleManage",
        component: () => import("@/views/manageAdmin/roleManage.vue"),
        name: "RoleManage",
        meta: {
          title: "教师管理" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      },
      {
        path: "courseManage",
        component: () => import("@/views/manageAdmin/courseManage.vue"),
        name: "CourseManage",
        meta: {
          title: "课程管理",
          roles: ["admin"] // 或者在子导航中设置角色
        }
      },
      {
        path: "notificationInfo",
        component: () => import("@/views/manageAdmin/notificationInfo.vue"),
        name: "NotificationInfo",
        meta: {
          title: "通知公告",
          roles: ["admin"]
        }
      },
      {
        path: "askForLeaveManage",
        name: "AskForLeaveManage",
        component: () => import("@/views/manageAdmin/askForLeaveManage.vue"),
        meta: {
          title: "请假管理",
          roles: ["admin"]
        }
      }
    ]
  },
  {
    path: "/teacherAdmin",
    component: Layouts,
    redirect: "/teacherAdmin/userManage",
    name: "TeacherAdmin",
    meta: {
      title: "老师管理",
      svgIcon: "lock",
      roles: ["teacher"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "courseManage",
        name: "CourseManageTeacher",
        component: () => import("@/views/teacherAdmin/courseManage.vue"),
        meta: {
          title: "课程管理",
          roles: ["teacher"] // 或者在子导航中设置角色
        }
      },
      {
        path: "askForLeaveManage",
        name: "AskForLeaveManageTeacher",
        component: () => import("@/views/teacherAdmin/askForLeaveManage.vue"),
        meta: {
          title: "请假管理",
          roles: ["teacher"]
        }
      },
      {
        path: "statisticalReport",
        name: "StatisticalReport",
        component: () => import("@/views/teacherAdmin/statisticalReport.vue"),
        meta: {
          title: "统计报表",
          roles: ["teacher"]
        }
      },
      {
        path: "notificationInfo",
        component: () => import("@/views/teacherAdmin/notificationInfo.vue"),
        name: "NotificationInfoTeacher",
        meta: {
          title: "通知公告",
          roles: ["teacher"]
        }
      },
      {
        path: "userManage",
        component: () => import("@/views/teacherAdmin/userManage.vue"),
        name: "UserManage",
        meta: {
          title: "用户管理", // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
          roles: ["teacher"]
        }
      }
    ]
  },
  {
    path: "/studentAdmin",
    component: Layouts,
    redirect: "/studentAdmin/userManage",
    name: "StudentAdmin",
    meta: {
      title: "学生管理",
      svgIcon: "lock",
      roles: ["student"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "courseManage",
        name: "CourseManageStudent",
        component: () => import("@/views/studentAdmin/courseManage.vue"),
        meta: {
          title: "课程管理",
          roles: ["student"] // 或者在子导航中设置角色
        }
      },
      {
        path: "askForLeaveManage",
        name: "AskForLeaveManageStudent",
        component: () => import("@/views/studentAdmin/askForLeaveManage.vue"),
        meta: {
          title: "请假管理",
          roles: ["student"]
        }
      },
      {
        path: "notificationInfo",
        component: () => import("@/views/studentAdmin/notificationInfo.vue"),
        name: "NotificationInfoStudent",
        meta: {
          title: "通知公告",
          roles: ["student"]
        }
      }
    ]
  },
  {
    path: "/permission",
    component: Layouts,
    redirect: "/permission/page",
    name: "Permission",
    meta: {
      title: "权限管理",
      svgIcon: "lock",
      roles: ["adminNot"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "page",
        component: () => import("@/views/permission/page.vue"),
        name: "PagePermission",
        meta: {
          title: "页面权限",
          roles: ["admin"] // 或者在子导航中设置角色
        }
      },
      {
        path: "directive",
        component: () => import("@/views/permission/directive.vue"),
        name: "DirectivePermission",
        meta: {
          title: "指令权限" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
